package com.heatup.apt.management.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.heatup.apt.common.dao.IndexResourceMapper;
import com.heatup.apt.management.service.IndexResourceService;
import com.heatup.apt.model.IndexResource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * IndexResource 表数据服务层接口实现类
 *
 */
@Service
public class IndexResourceServiceImpl extends SuperServiceImpl<IndexResourceMapper, IndexResource> implements IndexResourceService {

    @Resource
    private IndexResourceMapper indexResourceMapper;

    /**
     * 保存首页资源
     *
     * @param indexResource 保存参数
     * @return true 保存成功 / false 保存失败
     * @author blank 2017/7/13 下午12:28
     */
    @Override
    public boolean save(IndexResource indexResource) {

        if (indexResource != null) {
            indexResource.setStatus(2);
            if (indexResource.getId() == null) {
                if (indexResource.getSysUser() !=null && indexResource.getSysUser().getId() != null) {
                    indexResource.setCreatedBy(indexResource.getSysUser().getId());
                }
                indexResource.setCreatedTime(new Date());
                return indexResourceMapper.insertSelective(indexResource) > 0;

            } else {
                if (indexResource.getSysUser() !=null && indexResource.getSysUser().getId() != null) {
                    indexResource.setUpdatedBy(indexResource.getSysUser().getId());
                }
                indexResource.setUpdatedTime(new Date());
                return indexResourceMapper.updateSelectiveById(indexResource) > 0;
            }
        }
        return false;
    }

    /**
     * 查询首页资源
     *
     * @param indexResourceQuery 新首页查询参数对象
     * @author blank 2017/7/14 下午12:14
     */
    @Override
    public Map<String, List<IndexResource>> queryIndexResourceList(IndexResource indexResourceQuery) {


        List<IndexResource> indexResourceList = null;

        if (indexResourceQuery != null && indexResourceQuery.getMainType() != null) {
            EntityWrapper<IndexResource> entityWrapper = new EntityWrapper<>(indexResourceQuery);
            entityWrapper.orderBy("sort_num", true);
            if (indexResourceQuery.getMainType().equals(1) || indexResourceQuery.getMainType().equals(2)) {
                indexResourceList =  indexResourceMapper.selectList(entityWrapper);
            } else if (indexResourceQuery.getMainType().equals(3)) {
                //文章 因为需要返回教练名称 只能自写关联 sql
                indexResourceList = indexResourceMapper.queryListWithCoachName();
            }

        }

        List<IndexResource> listHomeBanner = new ArrayList<>();
        List<IndexResource> listStore = new ArrayList<>();
        List<IndexResource> listPrivateRecommend = new ArrayList<>();
        List<IndexResource> listVideo = new ArrayList<>();
        List<IndexResource> listArticleCoach = new ArrayList<>();
        List<IndexResource> listArticleNor = new ArrayList<>();
        List<IndexResource> listArticleNotice = new ArrayList<>();

        for (IndexResource indexResource : indexResourceList) {
            if (indexResource.getMainType() != null && indexResource.getMainType() == 1) {

                if (indexResource.getSubType() != null && indexResource.getSubType() == 1) {
                    listHomeBanner.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 2) {
                    listStore.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 3) {
                    listPrivateRecommend.add(indexResource);
                }
            }
            if (indexResource.getMainType() != null && indexResource.getMainType() == 2) {

                listVideo.add(indexResource);
            }
            if (indexResource.getMainType() != null && indexResource.getMainType() == 3) {

                if (indexResource.getSubType() != null && indexResource.getSubType() == 1) {
                    listArticleNor.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 2) {
                    listArticleCoach.add(indexResource);
                }
                if (indexResource.getSubType() != null && indexResource.getSubType() == 3) {
                    listArticleNotice.add(indexResource);
                }
            }
        }

        Map<String, List<IndexResource>> map = new HashMap();
        if (listHomeBanner.size() > 0) {
            map.put("listHomeBanner", listHomeBanner);
        }
        if (listStore.size() > 0) {
            map.put("listStore", listStore);
        }
        if (listPrivateRecommend.size() > 0) {
            map.put("listPrivateRecommend", listPrivateRecommend);
        }
        if (listVideo.size() > 0 ) {
            map.put("listVideo", listVideo);
        }
        if (listArticleNor.size() > 0) {
            map.put("listArticleNor", listArticleNor);
        }
        if (listArticleCoach.size() > 0) {
            map.put("listArticleCoach", listArticleCoach);
        }
        if (listArticleNotice.size() > 0) {
            map.put("listArticleNotice", listArticleNotice);
        }

        return map;
    }

    /**
     * 更新新资源排序
     *
     * @param sortString 需要更新的 ID 字符串
     * @return 更新数量
     * @author blank 2017/8/3 下午4:48
     */
    @Override
    public Integer updateSort(@RequestParam String sortString) {

        sortString = sortString.replace("##undefined", "");
        String [] IndexResourceIds = sortString.split("##");
        IndexResource indexResource;
        List<IndexResource> list = new ArrayList<>();
        for(int i = 1; i <= IndexResourceIds.length; i++){

            String idString  = IndexResourceIds[i-1];
            if (StringUtils.isNotBlank(idString)) {

                if (idString.contains("hf_")) {

                    idString = idString.replace("hf_", "");
                    String[] str = idString.split("_");
                    Integer id = Integer.valueOf(str[0]);

                    indexResource = new IndexResource();
                    indexResource.setId(id);
                    indexResource.setSortNum(i);
                    list.add(indexResource);
                }

            }
        }
        if ( list.size() > 0) {
            return indexResourceMapper.updateBatchById(list);
        }
        return 0;
    }
}